var fetch = require('@system.fetch');

var API_ROOT = 'https://route.showapi.com';
var headers = {
	'Content-Type': 'application/x-www-form-urlencoded'
};
var appid = '75912';
var secret = '2ba4fb8b1c204a2b9bee2b01898ea8fd';
import prompt from '@system.prompt'
function getTimestamp() {
	let nowDate = new Date();
	let hours = nowDate.getHours();
	let minutes = nowDate.getMinutes();
	let seconds = nowDate.getSeconds();
	if (hours < 10) {
		hours = '0' + String(hours);
	}
	if (minutes < 10) {
		minutes = '0' + String(minutes);
	}
	if (seconds < 10) {
		seconds = '0' + String(seconds);
	}
	return String(hours) + String(minutes) + String(seconds);
}

function realFetch(url, data = null, method = 'get') {
	if (data == null) {
		data = {};
	}
	return new Promise((resolve, reject) => {
		fetch.fetch({
			url: API_ROOT + url,
			data: data,
			header: headers,
			method: method,
			success: function(data) {
				resolve(data);
			},
			fail: function(data, code) {
                prompt.showDialog({
                    title: '请求出错！',
                    message: '请求出错，请稍后重新再试！'
                });
				reject(data, code);
			}
		});
	});
}

function post(url, data = null) {
	return realFetch(url, data, 'post');
}

function get(url, data = null) {
	return realFetch(url, data, 'get');
}

export  function getzixunType() {
		return post('/96-108',{
            showapi_appid:appid,
            showapi_sign:secret,
            showapi_timestamp:getTimestamp(),
            showapi_res_gzip:0
		}).then((response) => {
            return Promise.resolve(JSON.parse(response.data));
        }).catch((err) => {
            return Promise.reject(err);
	})
}


export  function getzixunTypeList(tid='',keywords='',page='') {
    return post('/96-109',{
        showapi_appid:appid,
        showapi_sign:secret,
        showapi_timestamp:getTimestamp(),
        showapi_res_gzip:0,
        tid:tid,
        page:page,
        keyword:keywords
    }).then((response) => {
        return Promise.resolve(JSON.parse(response.data));
    }).catch((err) => {
        return Promise.reject(err);
    })
}
export function getzixunInfo(id,key='') {
    return post('/96-36',{
        showapi_appid:appid,
        showapi_sign:secret,
        showapi_timestamp:getTimestamp(),
        showapi_res_gzip:0,
        id:id,
    }).then((response) => {
        var data = JSON.parse(response.data);
        data.before = key;
        return Promise.resolve(data);
    }).catch((err) => {
        return Promise.reject(err);
    })
}

export function getzishiList(tid='',keywords='',page=1) {
    return post('/90-87',{
        showapi_appid:appid,
        showapi_sign:secret,
        showapi_timestamp:getTimestamp(),
        showapi_res_gzip:0,
        tid:tid,
        page:page,
        key:keywords
    }).then((response) => {
        return Promise.resolve(JSON.parse(response.data));
    }).catch((err) => {
        return Promise.reject(err);
    })
}

export function getzishiInfo(id,key='') {
    return post('/90-88',{
        showapi_appid:appid,
        showapi_sign:secret,
        showapi_timestamp:getTimestamp(),
        showapi_res_gzip:0,
        id:id,
    }).then((response) => {
        var data = JSON.parse(response.data);
        data.before = key;
        return Promise.resolve(data);
    }).catch((err) => {
        return Promise.reject(err);
    })
}

export function getzishiType() {
    return post('/90-86',{
        showapi_appid:appid,
        showapi_sign:secret,
        showapi_timestamp:getTimestamp(),
    }).then((response) => {
        var data = JSON.parse(response.data);
        return Promise.resolve(data);
    }).catch((err) => {
        return Promise.reject(err);
    })
}

export  function GetJibinInfo(id,key='') {
    return post('/546-3',{
        showapi_appid:appid,
        showapi_sign:secret,
        showapi_timestamp:getTimestamp(),
        showapi_res_gzip:0,
        id:id,
    }).then((response) => {
        var data = JSON.parse(response.data);
        data.before = key;
        return Promise.resolve(data);
    }).catch((err) => {
        return Promise.reject(err);
    })
}
export  function GetJibinType() {
    return post('/546-1',{
        showapi_appid:appid,
        showapi_sign:secret,
        showapi_timestamp:getTimestamp(),
        showapi_res_gzip:0,
    }).then((response) => {
        var data = JSON.parse(response.data);
        return Promise.resolve(data);
    }).catch((err) => {
        return Promise.reject(err);
    })
}
export  function getBingList(subTypeId,key,page) {
    return post('/546-2',{
        showapi_appid:appid,
        showapi_sign:secret,
        showapi_timestamp:getTimestamp(),
        subTypeId:subTypeId,
        key:key,
        page:page

    }).then((response) => {
        var data = JSON.parse(response.data);
        return Promise.resolve(data);
    }).catch((err) => {
        return Promise.reject(err);
    })
}
export  function loc() {
    return new Promise((resolve, reject) => {
        fetch.fetch({
            url: 'http://dh38922.com/switch/api/get_loc',
            header: headers,
            method: 'get',
            success: function(response) {
                resolve(JSON.parse(response.data));
            },
            fail: function(data, code) {
                reject(data, code);
            }
        });
    });
}